<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
class Minime_Pubsubhubbub_MinimeSubscribers {
    
    private $subscribers = array();
    
    const ELEMENT_SUBSCRIBERS = "subscribers";
    
    public function __construct() {
        
    }

    public function addSubscriber(Minime_Pubsubhubbub_MinimeSubscriber  $sub) {
        $this->subscribers[$sub->getID()] = $sub;
    }
    
    public function lenght() {
        return count($this->subscribers);
    }
    
    // TODO add remove subscriber
    
    /**
     * Get subscriber 
     *
     * @param $id ID of te subscriber we are lookign for. 
     * @return Minime_Pubsubhubbub_Subscriber if subscription was found, false otherwise
     */
    public function getSubscriber($id) {
        if( isset($this->subscribers[$id]) ) {
            return $this->subscribers[$id];
        }
        return false;
    }
    
    public function getSubscribers() {
        return $this->subscribers;
    }
    
    public function asXml() {
        
        $doc = new DOMDocument('1.0', 'UTF-8');
        // we want a nice output
        $doc->formatOutput = true;

        $root = $doc->createElement(self::ELEMENT_SUBSCRIBERS);
        $root = $doc->appendChild($root);
        
        foreach($this->subscribers as $subscriber) {
            $root->appendChild($subscriber->asDOMElement($doc));
        }
        
        return $doc->saveXML();
    }
    
    /**
     * 
     * @param $dom
     * @return DOMElement
     */
    public function asDOMElement(DOMDocument $dom) {
        $root = $dom->createElement(self::ELEMENT_SUBSCRIBERS);
        foreach ($this->subscribers as $subscriber) {
            $root->appendChild($subscriber->asDOMElement($dom));
        }
        return $root;
    }
    
    /**
     * DOMElement passed here is reprosenting something siilar to this:
     * 
     * <subscribers>
     *   <subscriber>koski</subscriber>
     *   <subscriber>testkoski@identi.ca</subscriber>
     * </subscribers>
     * 
     * @return Minime_Pubsubhubbub_Subscribers 
     */
    public static function buildFromDom(DOMElement $elm) {
        
        $subscribers = new Minime_Pubsubhubbub_MinimeSubscribers();
        
        foreach ($elm->getElementsByTagName(Minime_Pubsubhubbub_MinimeSubscriber::ELEMENT_SUBSCRIBER) as $subscriber_elm) {
            $subscribers->addSubscriber(Minime_Pubsubhubbub_MinimeSubscriber::buildFromDOMElement($subscriber_elm));
        }
        
        return $subscribers;
    }
}